<template>
  <!-- 手机端路由过渡效果 -->
  <router-view v-slot="{ Component }">
    <transition name="fade-slide" mode="out-in">
      <component :is="Component" />
    </transition>
  </router-view>

  <van-tabbar v-model="active" v-if="showTabbar">
    <van-tabbar-item icon="wap-home" to="/">首页</van-tabbar-item>
    <van-tabbar-item icon="manager" to="/user">个人中心</van-tabbar-item>
  </van-tabbar>
</template>

<script setup>
import { useRouter } from 'vue-router';
import { ref, computed, watch, onMounted } from 'vue';

const router = useRouter();
const active = ref(0);
const currentRoute = router.currentRoute;

// 计算是否显示 Tabbar
const showTabbar = computed(() => {
  return !currentRoute.value.meta?.hideNav;
});

// 根据路由路径获取对应的 active 索引
const getActiveIndex = (path) => {
  if (path === '/') return 0; // 首页
  if (path === '/user') return 1; // 个人中心
  return 0; // 默认值
};

// 组件挂载时设置 active
onMounted(() => {
  active.value = getActiveIndex(currentRoute.value.path);
});

// 监听路由变化，更新 active
watch(() => currentRoute.value.path, (newPath) => {
  active.value = getActiveIndex(newPath);
});
</script>

<style>
/* 手机端路由过渡动画 */
.fade-slide-enter-active,
.fade-slide-leave-active {
  transition: all 0.3s ease;
}

.fade-slide-enter-from {
  opacity: 0;
  transform: translateX(20px);
}

.fade-slide-leave-to {
  opacity: 0;
  transform: translateX(-20px);
}
</style>